//
//  KABCelebrityTCell.m
//  kabu
//
//  Created by XiangDongMBP on 2017/2/4.
//  Copyright © 2017年 East. All rights reserved.
//

#import "KABCelebrityTCell.h"
#import "KABModel.h"
#import <YYWebImage.h>
#import "GlobalHeader.h"
#import "UIColor+XDHex.h"

@interface KABCelebrityTCell()
@property (nonatomic, strong) NSArray<UIImage *> *indexImgArrays;
@end

@implementation KABCelebrityTCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.contentBgHeightConstraint.constant = 0;
    
    self.indexImgArrays = @[[UIImage imageNamed:@"cosplay_activity_gold"],
                            [UIImage imageNamed:@"cosplay_activity_silver"],
                            [UIImage imageNamed:@"cosplay_activity_bronze"]];
    
    self.portraitImgView.layer.masksToBounds = YES;
    self.portraitImgView.layer.cornerRadius = CGRectGetWidth(self.portraitImgView.frame) / 2.0;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(portraitImgViewTapped:)];
    [self.portraitImgView addGestureRecognizer:tapGesture];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)imageViewDownLoad {
    if ([self.delegate respondsToSelector:@selector(kABCelebrityTCell:imgViewDownload:)]) {
        [self.delegate kABCelebrityTCell:self imgViewDownload:self.introduceImgView];
    }
}

- (void)portraitImgViewTapped:(UIGestureRecognizer *)gesture {
    if ([self.delegate respondsToSelector:@selector(kABCelebrityTCell:portraitImgViewTapped:)]) {
        [self.delegate kABCelebrityTCell:self portraitImgViewTapped:(UIImageView *)gesture.view];
    }
}

- (void)configViewWithCelebrityItem:(KABCosCelListItem *)item {
    UIImage *rankBgImg = nil;
    NSString *rankStr = nil;
    if (item.ranking_id <= 1000) {
        rankBgImg = self.indexImgArrays[0];
        rankStr = [NSString stringWithFormat:@"%zd", item.ranking_id];
    } else if (item.ranking_id <= 5000) {
        rankBgImg = self.indexImgArrays[1];
    } else {
        rankBgImg = self.indexImgArrays[2];
    }
    [self.rankBtn setBackgroundImage:rankBgImg forState:UIControlStateNormal];
    [self.rankBtn setTitle:rankStr forState:UIControlStateNormal];
    
    [self.portraitImgView yy_setImageWithURL:[NSURL URLWithString:item.headimg_url]
                                 placeholder:[KABDefaultVarModel sharedInstance].smallDefaultImg];
    
//    NSString *titleStr = @"合计：";
    NSString *priceStr = [NSString stringWithFormat:@"%@  |  %@", item.nick_name, item.city];
    NSMutableAttributedString *mAStr = [[NSMutableAttributedString alloc] initWithString:priceStr];
    NSDictionary *dic = @{NSForegroundColorAttributeName : [UIColor xd_colorWithHex:KABGlobalColorSettingFont],
                          NSFontAttributeName : [UIFont systemFontOfSize:12.0]};
    [mAStr addAttributes:dic range:NSMakeRange(item.nick_name.length, priceStr.length - item.nick_name.length)];
//    self.allPriceLabel.attributedText = mAStr;
    [self.nameBtn setAttributedTitle:mAStr forState:UIControlStateNormal];
//    [self.nameBtn setTitle:item.nick_name forState:UIControlStateNormal];
    [self.fansCntBtn setTitle:[NSString stringWithFormat:@" %zd", item.fans_number] forState:UIControlStateNormal];
    
    self.introNameLabel.text = item.nick_name;
    [self.introFansCntBtn setTitle:[NSString stringWithFormat:@" %zd", item.fans_number] forState:UIControlStateNormal];
    self.introduceLabel.text = item.introduce_text;
    
    // 展开or折叠
    self.contentBgHeightConstraint.priority = item.isExpanded ? 250 : 999;
    
    if (item.isExpanded) {
        self.introduceImgViewHeightConstraint.constant = 343.0;
        [self.introduceImgView yy_setImageWithURL:[NSURL URLWithString:item.introduce_image_url]
                                      placeholder:[KABDefaultVarModel sharedInstance].bigDefaultImg
                                          options:kNilOptions
                                       completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
//                                           NSLog(@"yy加载(%zd)完成BLOCK", self.index);
                                           if (image) {
                                               self.introduceImgViewHeightConstraint.constant = floor([UIScreen mainScreen].bounds.size.width * image.size.height / image.size.width);
                                               [self imageViewDownLoad];
                                           }
                                       }];
    }
}

@end
